﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="RouteConfig.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using InnovaApp.Layers.CommonLayer.Controllers;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using InnovaApp.Portals.MVC4Portal.Controllers;
using InnovaApp.Portals.MVC4Portal.Controllers.FriendlyURLHandling;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace InnovaApp.Portals.MVC4Portal
{
    /// <summary>
    /// Class RouteConfig
    /// </summary>
    public class RouteConfig
    {
        /// <summary>
        /// Registers the routes.
        /// </summary>
        /// <param name="routes">The routes.</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            string defaultLanguage = PortalEnvironment.DefaultLanguage;

            List<Language> Languages = new List<Language>();

            using (DataRepositories _dataContext = new DataRepositories())
            {
                Languages = _dataContext.Language_Rep.GetAll().OrderBy(x => x.SortIndex).ToList();
            }

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.IgnoreRoute("Scripts/{*pathInfo}");
            routes.IgnoreRoute("bundles/{*pathInfo}");
            routes.IgnoreRoute("Content/{*pathInfo}");

            routes.MapRoute(
                "RSSFeed", // Route name
                "rss.xml", // URL with parameters
                new { controller = "RSS", action = "index", type = "xml" } // Parameter defaults
            );

            routes.MapRoute(
                "DynamicJS", // Route name
                "scriptsg/{language}/{action}.js", // URL with parameters
                new { controller = "ScriptsG", action = "inoHomeDynamic", language = CommonEnvironment.DefaultLanguage, type = "js" } // Parameter defaults
            );

            routes.MapRoute(
                "DynamicCSS", // Route name
                "cssg/{action}.css", // URL with parameters
                new { controller = "CSSG", action = "Site", type = "css" } // Parameter defaults
            );

            routes.MapRoute(
                "LogXML", // Route name
                "Logs/{action}.xml", // URL with parameters
                new { controller = "LogXMLFile", action = "index", type = "xml" } // Parameter defaults
            );

            routes.MapRoute(
                name: "LogOff",
                url: "logoff/{action}",
                defaults: new { controller = "LogOff", action = "Index" }
            );

            routes.MapRoute(
                name: "LogOn",
                url: "LogOn/{action}",
                defaults: new { controller = "LogOn", action = "SelectLanguage" }
            );

            routes.MapRoute(
                name: "ForgotPassword",
                url: "ForgotPassword/{action}",
                defaults: new { controller = "ForgotPassword", action = "SelectLanguage" }
            );

            routes.MapRoute(
                name: "LayoutManager",
                url: "LayoutManager/{action}",
                defaults: new { controller = "LayoutManager", action = "GetLayout" }
            );

            foreach (var l in Languages)
            {

                #region Error
                routes.MapRoute(
                    name: "Error" + l.Name.ToUpper(),
                    url: l.Description + "/Error/{action}/{id}",
                    defaults: new { controller = "Error", action = "Index", id = UrlParameter.Optional, language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();
                #endregion

                #region LogViewer
                routes.MapRoute(
                  name: "LogViewer" + l.Name.ToUpper(),
                  url: l.Description + "/LogViewer/{id}",
                  defaults: new { controller = "LogViewer", action = "Index", id = UrlParameter.Optional }
                ).RouteHandler = new LocalizedURLHandler();
                #endregion

                #region Membership
                routes.MapRoute(
                    name: "ForgotPassword" + l.Name.ToUpper(),
                    url: l.Description + "/ForgotPassword/{action}",
                    defaults: new { controller = "ForgotPassword", action = "Index", language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "ResetForgottenPassword" + l.Name.ToUpper(),
                    url: l.Description + "/ResetForgottenPassword/{action}",
                    defaults: new { controller = "ResetForgottenPassword", action = "Index", language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "LogOn" + l.Name.ToUpper(),
                    url: l.Description + "/logon/{action}",
                    defaults: new { controller = "LogOn", action = "Index", language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "Register" + l.Name.ToUpper(),
                    url: l.Description + "/register/{action}",
                    defaults: new { controller = "Register", action = "Index", language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "ChangePassword" + l.Name.ToUpper(),
                    url: l.Description + "/ChangePassword/{action}",
                    defaults: new { controller = "ChangePassword", action = "Index", language = l.Description }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "AccountActivation" + l.Name.ToUpper(),
                    url: l.Description + "/AccountActivation/{action}",
                    defaults: new { controller = "AccountActivation", action = "Index" }
                ).RouteHandler = new LocalizedURLHandler();

                #endregion

                #region Asset management
                routes.MapRoute(
                    name: "AssetsManager" + l.Name.ToUpper(),
                    url: l.Description + "/AssetsManager/{action}/{auth}",
                    defaults: new { controller = "AssetsManager", action = "Index", language = l.Description, auth = UrlParameter.Optional }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "connector" + l.Name.ToUpper(),
                    url: l.Description + "/Connector/{action}",
                    defaults: new { controller = "ELFinderConnector", action = "Index" }
                ).RouteHandler = new LocalizedURLHandler();

                routes.MapRoute(
                    name: "Finder" + l.Name.ToUpper(),
                    url: l.Description + "/Finder/{action}/{auth}",
                    defaults: new { controller = "Finder", action = "Index", language = l.Description, auth = UrlParameter.Optional }
                ).RouteHandler = new LocalizedURLHandler();

                #endregion

                #region Structure management
                routes.MapRoute(
                    name: "LayoutMenuEditor" + l.Name.ToUpper(),
                    url: l.Description + "/LayoutMenuEditor/{id}",
                    defaults: new { controller = "LayoutMenuEditor", action = "Index", id = UrlParameter.Optional }
                ).RouteHandler = new LocalizedURLHandler();
                #endregion

                #region PlugInAccessor
                routes.MapRoute(
                    name: "PlugInAccessor" + l.Name.ToUpper(),
                    url: l.Description + "/PlugInAccessor/{id}",
                    defaults: new { controller = "PlugInAccessor", action = "Index", id = UrlParameter.Optional }
                ).RouteHandler = new LocalizedURLHandler();
                #endregion
            }

            routes.MapRoute(
                name: "SaveContent",
                url: "DynaContent/{action}/{id}",
                defaults: new { controller = "DynaContent", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "SaveStructure",
                url: "DynaStructure/{action}/{language}",
                defaults: new { controller = "DynaStructure", action = "Index", language = "" }
            );

            routes.MapRoute(
                name: "ChangePassword",
                url: "ChangePassword/{action}/{language}",
                defaults: new { controller = "ChangePassword", action = "", language = "" }
            );

            routes.MapRoute(
                name: "SavePublicContent",
                url: "DynaContentPublic/Save/{id}",
                defaults: new { controller = "DynaContentPublic", action = "Save", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "SavePrivateContent",
                url: "DynaContentPrivate/Save/{id}",
                defaults: new { controller = "DynaContentPrivate", action = "Save", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "MyProfile",
                url: "MyProfile/{action}/{language}",
                defaults: new { controller = "MyProfile", action = "GetCurrentUserProfile" }
            );

            routes.MapRoute(
                name: "Lookups",
                url: "Lookups/{action}/{language}",
                defaults: new { controller = "Lookups", action = "" }
            );

            routes.MapRoute(
                name: "Admin",
                url: "Admin/{action}/{language}",
                defaults: new { controller = "Admin", action = "" }
            );

            routes.MapRoute(
                name: "AdminSettings",
                url: "AdminSettings/{action}/{language}",
                defaults: new { controller = "AdminSettings", action = "" }
            );

            routes.MapRoute(
                name: "SingleFileUpload",
                url: "SingleFileUpload/{action}",
                defaults: new { controller = "SingleFileUpload", action = "" }
            );

            routes.MapRoute
            (
                "Friendly",
                "{*FriendlyUrl}"
            ).RouteHandler = new FriendlyUrlRouteHandler();
        }
    }
}